<?php
// 本类由系统自动生成，仅供测试用途
/**
 * IndexAction
 * 
 * @package 
 * @author www.80boys.com
 * @copyright 2012
 * @version $Id$
 * @access public
 */
class IndexAction extends CommonAction {
	public function index(){
		//载入菜单列表
        #添加菜单权限
        $menu = self::apppriv();              
        $this->assign('menu',   $menu );
	    	$this->assign('admin',session('_LOGIN_'));
        $this->display();  
    }
	public function main(){
       $user = session('_LOGIN_');
       $this->assign('admin',session('_LOGIN_'));
       $_SERVER['now'] = date('Y-m-d H:i:s');
       $this->assign('server',$_SERVER);      
       $this->display();  
	}
    #获取顶级菜单
    protected function getchannel(){
       $channel =M("App")->where('app_depth = 1')->field(" app_id ,app_name,app_role")->select();
       return $channel;
    }
    #二级三级菜单
   protected function getmenu(){
       $list = $this->getchannel(); 
       #二级分类
       foreach($list as $k => $v){
        $arr =M("App")->where('app_depth = 2 AND app_fid='.$v['app_id'])->field(" app_id ,app_name,app_role")->select();
        $list[$k]['child'] = $arr;
       }
       #三级分类
       foreach($list as $k => $v){
        foreach($v['child'] as $kk => $vv){
          $arr =M("App")->where('app_depth = 3 AND app_fid='.$vv['app_id'])->field(" app_id ,app_name,app_role,app_url")->select();  
          $list[$k]['child'][$kk]['child'] = $arr ;  
        }
       }
       //print_r($list);
        return $list;
   }
   #检查菜单权限
   private function checkApp($str=''){
    $role = @explode(',',$str);
    $user = session('_LOGIN_');
    foreach($user['rolelist'] as $k => $v){
          if(in_array($v['role_id'],$role)){      
                 return true;
          }   
    }
      return false;
   }
    # 菜单赋权限
   private function apppriv(){
            $menu = $this->getmenu();
            foreach($menu as $k => $v)
               {
                #一级分类
                 if(self::checkApp($v['app_role'])){
                    $menu[$k]['isapp'] = true ;
                 }else{
                        $menu[$k]['isapp'] = false ;                  
                 }
                #二级分类
                foreach($v['child'] as $kk => $vv){
                       if(self::checkApp($vv['app_role'])){
                                           $menu[$k]['child'][$kk]['isapp'] = true ;
                        }else{
                                $menu[$k]['child'][$kk]['isapp'] = false ;                  
                        }
                #三级分类
                foreach($vv['child'] as $kkk => $vvv){
                       if(self::checkApp($vvv['app_role'])){
                                           $menu[$k]['child'][$kk]['child'][$kkk]['isapp'] = true ;
                        }else{
                                $menu[$k]['child'][$kk]['child'][$kkk]['isapp'] = false ;                  
                        }
                }
                }
   }
   return $menu;
    }
    # 菜单赋权限
   public function setting(){
    if($this->_post('sub')){
     $data2['webtitle'] = $this->_post('webtitle');
     $data2['webmeta'] = $this->_post('webmeta');
     $data2['webkeywords'] = $this->_post('webkeywords');
     $data2['webstatus'] = $this->_post('webstatus');
     $data2['webverify'] = $this->_post('webverify');
     $data2['closeanswer'] = $this->_post('closeanswer');
     $data['value'] = json_encode($data2);
      M('Setting')->where(" style = 'system' ")->save($data);
      $this->success('更新成功!','setting');
      exit();
    }
    $system = M('Setting')->where(" style = 'system' ")->field('*')->find();
    $this->assign('system', json_decode($system['value'],true));
    $this->display();
   }
}